using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using DaedalusEngine.Source;

namespace DaedalusX
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SpriteFont font;

        Actor princess;
        Actor princess2;

        Vector2 acceleration;
        Vector2 velocity;
        float yThreshold;
        bool jumping = false;
        bool contact = true;

        KeyboardState ks;
        KeyboardState prevKS;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //princess = new AnimatedSprite("Princess",
            //    Content.Load<Texture2D>("PrincessCharacter"), new Vector2(200, 200), 0.0f,
            //    Color.White, 1.0f, .4f);

           

            font = Content.Load<SpriteFont>("font");

            princess = new Actor(Content.Load<Texture2D>("PrincessCharacter"), new Vector2(200, 200),
                0.0f, 1.0f, 1.0f, Color.White);
            princess2 = new Actor(Content.Load<Texture2D>("PrincessCharacter"), new Vector2(400, 200),
                0.0f, 1.0f, 1.0f, Color.Red);

            princess.AddAnimation("Left", 0, 0, 32, 64, 4, 0.2f, null);
            princess.AddAnimation("Right", 0, 64, 32, 64, 4, 0.2f, null);
            princess.AddAnimation("IdleLeft", 0, 0, 32, 64, 1, 0.2f, null);
            princess.AddAnimation("IdleRight", 32, 64, 32, 64, 1, 0.2f, null);
            princess2.AddAnimation("Left", 0, 0, 32, 64, 4, 0.2f, null);
            princess2.AddAnimation("Right", 0, 64, 32, 64, 4, 0.2f, null);
            princess2.AddAnimation("IdleLeft", 0, 0, 32, 64, 1, 0.2f, null);
            princess2.AddAnimation("IdleRight", 32, 64, 32, 64, 1, 0.2f, null);

            yThreshold = 200f;

            acceleration.Y = -9.8f;

            //Console.WriteLine(princess.ToString());
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            ks = Keyboard.GetState();

            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            princess.Update(gameTime);
            princess2.Update(gameTime);

            


            if (ks.IsKeyDown(Keys.A))
            {
                acceleration.X = -0.4f;
                if (princess.CurrentAnimation != "Left")
                    princess.CurrentAnimation = "Left";
            }
            else if (ks.IsKeyDown(Keys.D))
            {
                acceleration.X = 0.4f;
                if (princess.CurrentAnimation != "Right")
                    princess.CurrentAnimation = "Right";
            }
            else
            {
                if (princess.CurrentAnimation == "Left")
                    princess.CurrentAnimation = "IdleLeft";
                if (princess.CurrentAnimation == "Right")
                    princess.CurrentAnimation = "IdleRight";
            }
            if (ks.IsKeyUp(Keys.A) && ks.IsKeyUp(Keys.D))
                acceleration.X = 0.0f;

            if (ks.IsKeyDown(Keys.Space) && contact)
            {
                velocity.Y = 5.5f;
                contact = false;
                jumping = true;
            }

            velocity.X *= 0.85f;

            if (jumping)
            {
                velocity.Y += acceleration.Y * dt;
                if (princess.position.Y > yThreshold)
                {
                    velocity.Y = 0.0f;
                    princess.position.Y = yThreshold;
                    contact = true;
                    jumping = false;
                }
            }

            

            velocity.X += acceleration.X * dt;
            princess.position.X += velocity.X * 50.0f;
            princess.position.Y -= velocity.Y;
            
            prevKS = ks;
            
            base.Update(gameTime);
        }

       

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            princess.Draw(spriteBatch);
            princess.SetBoundRect(-12, -5);
            princess2.Draw(spriteBatch);
            princess2.SetBoundRect(-12, -5);

            if (princess.RectCollidesWith(princess2))
                spriteBatch.DrawString(font, "Collision Detected", new Vector2(50, 50), Color.White);
            spriteBatch.DrawString(font, princess.position.ToString(), new Vector2(10, 10), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
